<?php

/**
 * Behavior for binding and translation management
 *
 * PHP versions 5
 * CAKEPHP versions 2.x
 * 
 * Green CMS - Content Management System and Framework Powerfull by Cakephp
 * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
 * 
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author        Technology Lab No.I <tech1@toancauxanh.vn>
 * @link          
 * @package       Green.Model.Behavior
 * @since         Green v 1.0
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('ModelBehavior', 'Model/Behavior');

class MetaBehavior extends ModelBehavior {

    public function afterSave(Model $Model, $created) {
        if (!empty($Model->data['Meta']) && array_filter($Model->data['Meta'])) {
            $MetaModel = ClassRegistry::init('Meta');
            $data = array('model' => $Model->alias, 'foreign_key' => $Model->id);
            $last = $MetaModel->find('first', array(
                'conditions' => $data));
            $MetaModel->create();
            if ($last) {
                $MetaModel->id = $last['Meta']['id'];
            }
            $MetaModel->save(array_merge(array(
                        'content' => serialize($Model->data['Meta'])), $data));
        }
        return true;
    }

    public function afterDelete(Model $Model) {
        ClassRegistry::init('Meta')->deleteAll(array(
            'model' => $Model->alias, 'foreign_key' => $Model->id
        ));
        return true;
    }

}

?>